//获取应用实例
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cardNum:'',
    jifen:'',
    teamCount:'',
    jibie:'',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    menuitems: [{
      url: '../myInvite/myInvite',
      img: '../../images/yaoqing.png',
      text: '我的邀请'
      },{
      url:'../message/message',
      img:'../../images/mes.png',
      text:'系统消息'
      }, {
        url: '../achievement/achievement',
        img: '../../images/fx.png',
        text: '我的业绩'
      }, {
        url: '../address/address',
        img: '../../images/address.png',
        text: '收货地址'
      }, {
        url: '../collect/collect',
        img: '../../images/sc1.png',
        text: '我的收藏'
      }, {
        url: '../shoppingCart/shoppingCart',
        img: '../../images/gouwuche.png',
        text: '我的购物车'
      }, {
        url: '../QRcodeList/QRcodeList',
        img: '../../images/code.png',
        text: '我的二维码'
      }, {
        url: '../problem/problem',
        img: '../../images/problem.png',
        text: '常见问题'
      }, {
        url: '../cAndF/cAndF',
        img: '../../images/edit1.png',
        text: '投诉与反馈'
      }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    if (app.globalData.userInfo) {
      that.setUserInfo(app.globalData.userInfo);
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setUserInfo(res.userInfo);
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          that.setUserInfo(res.userInfo);
        }
      })
    }
    
    

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        
        wx.request({
          url: app.globalData.URL + '/app/personal/core?openid='+ openid,
          success: function (res) {
            console.log('personal/core',res.data)
            that.setData({
              cardNum: res.data.cardNum,
              jifen: res.data.jifen,
              teamCount: res.data.teamCount,
              jibie: res.data.jibie
            })
          }
        })
      },
    })
    
  },

  getUserInfo: function (e) {
    this.setUserInfo(e.detail.userInfo);  
  },

  setUserInfo: function (userInfo) {
    if (userInfo != null) {
      app.globalData.userInfo = userInfo
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
    }
  },
  toPoints:function(){
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/personal/qiandao?openid='+ openid,
          success: function (res) {
            if (res.data.flag == true) {
              wx.showModal({
                title: '',
                content: '恭喜您签到成功，获取1积分',
                success: function (res) {

                }
              })
            } else if (res.data.flag == false) {
              wx.showModal({
                title: '',
                content: '今天已经签到过了，请明天再来',
                success: function (res) {

                }
              })
            }
          }
        })
      },
    })
      
  },
  dingyue:function(){
    console.log("点击订阅")
    wx.requestSubscribeMessage({
      tmplIds: ['zwEH_R8zPHbtbHIcFtdxXZq56ySjqdc5KVuKzKzIGQE','0qc8Y0aSeFTzT5wsYLWIsf3KsNF0cnGXsPQItaLrY-8'],
      success(res) {
        console.log("订阅成功",res)
        if (res['zwEH_R8zPHbtbHIcFtdxXZq56ySjqdc5KVuKzKzIGQE'] === 'accept') {
          wx.showToast({
            title: '订阅成功!',
          })
        }
      },
      faile(res) {
        console.log('订阅faile', res)
      }
    })
  },

  toMyInfo:function(){
    wx.navigateTo({
      url: '../myInfo/myInfo'
    })
  }
})
